package com.ruizhong.PIW.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.PIW.dao.TDisplayMapper;
import com.ruizhong.PIW.entity.TDisplay;
import com.ruizhong.PIW.service.DisplayService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class DisplayServiceImpl extends ServiceImpl<TDisplayMapper, TDisplay> implements DisplayService {
    @Override
    public synchronized void submitUpdate(TDisplay tDisplay) {
        if (tDisplay.getId() == null) {
            save(tDisplay);
            return;
        }
        updateById(tDisplay);
    }

    @Override
    public TDisplay getDisplayInfo() {
        List<TDisplay> list = list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        TDisplay tDisplay = list.get(0);
//        String textAnimation = tDisplay.getTextAnimation();
//        String aimTextAnimation=null;
//        if (textAnimation!=null){
//            switch (textAnimation){
//                case "静态":
//                    aimTextAnimation="none";
//                    break;
//                case "从左向右":
//                    aimTextAnimation="right";
//                    break;
//                case "从右向左":
//                    aimTextAnimation="left";
//                    break;
//                case "从上到下":
//                    aimTextAnimation="down";
//                    break;
//                case "从下到上":
//                    aimTextAnimation="up";
//                    break;
//            }
//            tDisplay.setTextAnimation(aimTextAnimation);
//        }
        return tDisplay;
    }

    @Override
    public void updateDisplayInfo(TDisplay tDisplay) {
        Integer displayId = tDisplay.getId();
        if (displayId == null) {
            synchronized (this){
                List<TDisplay> list = list();
                if (list.size() == 0){
                    save(tDisplay);
                }else{
                    tDisplay.setId(list.get(0).getId());
                }
            }
        }
//        修改tDisplay
        boolean success = updateById(tDisplay);
        if (!success){
            throw new RuntimeException("修改失败");
        }

    }
}
